package com.esd.controller.bjlhdx;

import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.esd.csair.CS;
import com.esd.csair.Csair;
import com.esd.csair.CsairImpl;
import com.esd.csair.CsairReturnValue;

/**
 * 
 * 主页模块
 * 
 * @author snailzhang
 * @date 2017年7月20日
 *
 */
@Controller
@RequestMapping(value = "cs")
public class CsIndexController {
	private static Logger log = Logger.getLogger(CsIndexController.class);
	/**
	 * 新主页
	 * 
	 * @param session
	 * @return 2017年7月20日
	 */
	@RequestMapping(value = "index", method = RequestMethod.GET)
	public String index2(HttpSession session) {
		
		Object obj = session.getAttribute(Csair.USERNAME);
		log.info("obj====>"+obj);
		if (obj == null) {
			CsairImpl csair = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
			csair.getWebClient().close();
			log.info("重新打开首页");
			session.setAttribute(Csair.WEBCLIENT, new CsairImpl());
		} else {
			CsairImpl csair = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		}
		return "csair/jszlpj";
	}
	
	/**
	 * 北京联合大学无障碍登录预加载
	 * @param session
	 */
	@RequestMapping(value = "preloginBJLH", method = RequestMethod.POST)
	@ResponseBody
	public void preloginBJLH(HttpSession session) {
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		webClient.preloginBJLH();
	}
	
	/**
	 * 北京联合大学无障碍登录
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "loginBJLH", method = RequestMethod.POST)
	@ResponseBody
	public Map<String , Object> loginBJLH(HttpServletRequest request, HttpSession session) {
		log.info("===============登     录====================");
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		String num = request.getParameter("num");
		String psd = request.getParameter("psd");
		webClient.preLogin();
		Map<String , Object> map = webClient.loginBJLH(num, psd);
		return map;
	}

	/**
	 * 登录后跳转页
	 * @return
	 */
	@RequestMapping(value = "choise", method = RequestMethod.GET)
	public ModelAndView choise(HttpServletRequest request) {
		log.info("===============登录后跳转页====================");
		Map<String, Object> map = new HashMap<String ,Object>();
		String num = request.getParameter("num");
		String time = request.getParameter("time");
		map.put("num", num);
		map.put("time", time);
		return new ModelAndView("csair/choise",map);
	}
	/**
	 * 学生课表查询页面
	 * @return
	 */
	@RequestMapping(value = "xskbcx", method = RequestMethod.GET)
	public ModelAndView xskbcx(HttpServletRequest request) {
		log.info("===============学生课表查询页面====================");
		Map<String, Object> map = new HashMap<String ,Object>();
		String num = request.getParameter("num");
		map.put("num", num);
		return new ModelAndView("csair/gerenkechengbiao",map);
	}
	
	/**
	 * 学生课表查询加载
	 * @return
	 */
	@RequestMapping(value = "loadgerenkechengbiao", method = RequestMethod.GET)
	@ResponseBody
	public ModelAndView loadgerenkechengbiao(HttpServletRequest request,HttpSession session) {
		log.info("===============学生课表查询加载====================");
		Map<String, Object> map = new HashMap<String ,Object>();
		String num = request.getParameter("num");
		map.put("num", num);
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		map = webClient.loadgerenkechengbiao(num);
		return new ModelAndView("csair/loadgerenkechengbiao",map);
	}
	
	/**
	 * 学年select学生课表查询加载
	 * @return
	 */
	@RequestMapping(value = "xnselectloadgerenkechengbiao", method = RequestMethod.GET)
	@ResponseBody
	public ModelAndView xnselectloadgerenkechengbiao(HttpServletRequest request,HttpSession session) {
		log.info("===============学年变更select学生课表查询加载====================");
		Map<String, Object> map = new HashMap<String ,Object>();
		String xnd = request.getParameter("xnd");
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		map = webClient.xnselectloadgerenkechengbiao(xnd);
		return new ModelAndView("csair/loadgerenkechengbiao",map);
	}
	/**
	 * 学期select学生课表查询加载
	 * @return
	 */
	@RequestMapping(value = "xqselectloadgerenkechengbiao", method = RequestMethod.GET)
	@ResponseBody
	public ModelAndView xqselectloadgerenkechengbiao(HttpServletRequest request,HttpSession session) {
		log.info("===============学期变更select学生课表查询加载====================");
		Map<String, Object> map = new HashMap<String ,Object>();
		String xqd = request.getParameter("xqd");
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		map = webClient.xqselectloadgerenkechengbiao(xqd);
		return new ModelAndView("csair/loadgerenkechengbiao",map);
	}

	/**
	 * 成绩查询加载
	 * @return
	 */
	@RequestMapping(value = "cjcx", method = RequestMethod.GET)
	public ModelAndView cjcx(HttpServletRequest request) {
		log.info("===============学生课表查询页面====================");
		Map<String, Object> map = new HashMap<String ,Object>();
		String num = request.getParameter("num");
		map.put("num", num);
		return new ModelAndView("csair/cjcx",map);
	}
	
	
	/**
	 * 成绩查询加载
	 * @return
	 */
	@RequestMapping(value = "loadcjcx", method = RequestMethod.GET)
	@ResponseBody
	public ModelAndView loadcjcx(HttpServletRequest request,HttpSession session) {
		log.info("===============成绩查询加载====================");
		Map<String, Object> map = new HashMap<String ,Object>();
		String num = request.getParameter("num");
		map.put("num", num);
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		map = webClient.loadcjcx(num);
		return new ModelAndView("csair/loadwtgorkczgchengji",map);
	}
	
	/**
	 * 最高成绩查询加载
	 * @return
	 */
	@RequestMapping(value = "loadzgcjcx", method = RequestMethod.GET)
	@ResponseBody
	public ModelAndView loadzgcjcx(HttpServletRequest request,HttpSession session) {
		log.info("===============最高成绩查询加载====================");
		Map<String, Object> map = new HashMap<String ,Object>();
		String num = request.getParameter("num");
		String ddlXN = request.getParameter("ddlXN");
		String ddlXQ = request.getParameter("ddlXQ");
		String ddl_kcxz = request.getParameter("ddl_kcxz");
		
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		map = webClient.loadzgcjcx(num,ddlXN,ddlXQ,ddl_kcxz);
		return new ModelAndView("csair/loadwtgorkczgchengji",map);
	}
	
	/**
	 * 未通过成绩查询加载
	 * @return
	 */
	@RequestMapping(value = "loadwtgcjcx", method = RequestMethod.GET)
	@ResponseBody
	public ModelAndView loadwtgcjcx(HttpServletRequest request,HttpSession session) {
		log.info("===============未通过成绩查询加载====================");
		Map<String, Object> map = new HashMap<String ,Object>();
		String num = request.getParameter("num");
		String ddlXN = request.getParameter("ddlXN");
		String ddlXQ = request.getParameter("ddlXQ");
		String ddl_kcxz = request.getParameter("ddl_kcxz");
		
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		map = webClient.loadwtgcjcx(num,ddlXN,ddlXQ,ddl_kcxz);
		return new ModelAndView("csair/loadwtgorkczgchengji",map);
	}
	
	/**
	 * 学期成绩查询加载
	 * @return
	 */
	@RequestMapping(value = "loadxqcjcx", method = RequestMethod.GET)
	@ResponseBody
	public ModelAndView loadxqcjcx(HttpServletRequest request,HttpSession session) {
		log.info("===============学期成绩查询加载====================");
		Map<String, Object> map = new HashMap<String ,Object>();
		String num = request.getParameter("num");
		String ddlXN = request.getParameter("ddlXN");
		String ddlXQ = request.getParameter("ddlXQ");
		String ddl_kcxz = request.getParameter("ddl_kcxz");
		
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		map = webClient.loadxqcjcx(num,ddlXN,ddlXQ,ddl_kcxz);
		return new ModelAndView("csair/loadxqorxnorlnchengji",map);
	}
	
	/**
	 * 学年成绩查询加载
	 * @return
	 */
	@RequestMapping(value = "loadxncjcx", method = RequestMethod.GET)
	@ResponseBody
	public ModelAndView loadxncjcx(HttpServletRequest request,HttpSession session) {
		log.info("===============学年成绩查询加载====================");
		Map<String, Object> map = new HashMap<String ,Object>();
		String num = request.getParameter("num");
		String ddlXN = request.getParameter("ddlXN");
		String ddlXQ = request.getParameter("ddlXQ");
		String ddl_kcxz = request.getParameter("ddl_kcxz");
		
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		map = webClient.loadxncjcx(num,ddlXN,ddlXQ,ddl_kcxz);
		return new ModelAndView("csair/loadxqorxnorlnchengji",map);
	}
	
	/**
	 * 历年成绩查询加载
	 * @return
	 */
	@RequestMapping(value = "loadlncjcx", method = RequestMethod.GET)
	@ResponseBody
	public ModelAndView loadlncjcx(HttpServletRequest request,HttpSession session) {
		log.info("===============历年成绩查询加载====================");
		Map<String, Object> map = new HashMap<String ,Object>();
		String num = request.getParameter("num");
		String ddlXN = request.getParameter("ddlXN");
		String ddlXQ = request.getParameter("ddlXQ");
		String ddl_kcxz = request.getParameter("ddl_kcxz");
		
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		map = webClient.loadlncjcx(num,ddlXN,ddlXQ,ddl_kcxz);
		return new ModelAndView("csair/loadxqorxnorlnchengji",map);
	}
	
	/**
	 * 成绩统计
	 * @return
	 */
	@RequestMapping(value = "loadcjtj", method = RequestMethod.GET)
	@ResponseBody
	public ModelAndView loadcjtj(HttpServletRequest request,HttpSession session) {
		log.info("===============成绩统计加载====================");
		Map<String, Object> map = new HashMap<String ,Object>();
		String num = request.getParameter("num");
		String ddlXN = request.getParameter("ddlXN");
		String ddlXQ = request.getParameter("ddlXQ");
		String ddl_kcxz = request.getParameter("ddl_kcxz");
		
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		map = webClient.loadcjtj(num,ddlXN,ddlXQ,ddl_kcxz);
		return new ModelAndView("csair/loadchengjitongji",map);
	}
	
	/**
	 * 教师质量评价
	 * @return
	 */
	@RequestMapping(value = "jszlpj", method = RequestMethod.GET)
	public ModelAndView jszlpj(HttpServletRequest request) {
		log.info("===============教师质量评价页面====================");
		Map<String, Object> map = new HashMap<String ,Object>();
		String num = request.getParameter("num");
		map.put("num", num);
		return new ModelAndView("csair/jszlpj",map);
	}
	
	/**
	 * 教师质量评价加载
	 * @return
	 */
	@RequestMapping(value = "loadjszlpj", method = RequestMethod.GET)
	@ResponseBody
	public ModelAndView loadjszlpj(HttpServletRequest request,HttpSession session) {
		log.info("===============教师质量评价加载====================");
		Map<String, Object> map = new HashMap<String ,Object>();
		String num = request.getParameter("num");
		map.put("num", num);
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		map = webClient.loadjszlpj(num);
		return new ModelAndView("csair/loadjszlpj",map);
	}
	
	/**
	 * 课程select变更
	 * @return
	 */
	@RequestMapping(value = "kcChange", method = RequestMethod.GET)
	@ResponseBody
	public ModelAndView kcChange(HttpServletRequest request,HttpSession session) {
		log.info("===============课程select变更====================");
		Map<String, Object> map = new HashMap<String ,Object>();
		String pjkc = request.getParameter("pjkc");
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		map = webClient.kcChange(pjkc);
		return new ModelAndView("csair/loadjszlpj",map);
	}
	
	/**
	 * 保存成功后页面变更
	 * @return
	 */
	@RequestMapping(value = "baocunSuccessKcChange", method = RequestMethod.GET)
	@ResponseBody
	public ModelAndView baocunSuccessKcChange(HttpServletRequest request,HttpSession session) {
		log.info("===============保存成功后页面变更====================");
		Map<String, Object> map = new HashMap<String ,Object>();
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		map = webClient.baocunSuccessKcChange();
		return new ModelAndView("csair/loadjszlpj",map);
	}
	
	/**
	 * 课程类型select变更
	 * @return
	 */
	@RequestMapping(value = "kclxChange", method = RequestMethod.GET)
	@ResponseBody
	public ModelAndView kclxChange(HttpServletRequest request,HttpSession session) {
		log.info("===============课程类型select变更====================");
		Map<String, Object> map = new HashMap<String ,Object>();
		String kclx = request.getParameter("kclx");
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		map = webClient.kclxChange(kclx);
		return new ModelAndView("csair/loadjszlpj",map);
	}
	
	/**
	 * 具体教师评价onblur事件
	 * @return
	 */
	@RequestMapping(value = "pingjia", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> pingjia(HttpServletRequest request,HttpSession session) {
		log.info("===============具体教师评价onblur事件====================");
		Map<String, Object> map = new HashMap<String ,Object>();
		String pingjiaValue = request.getParameter("pingjiaValue").toString();
		String pingjiaId = request.getParameter("pingjiaId").toString();
		byte[] base64decodedBytes1 = Base64.getDecoder().decode(pingjiaValue);
		byte[] base64decodedBytes2 = Base64.getDecoder().decode(pingjiaId);
		try {
			pingjiaValue = new String(base64decodedBytes1, "utf-8");
			pingjiaId = new String(base64decodedBytes2, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		System.out.println("pingjiaValue===>"+pingjiaValue);
		System.out.println("pingjiaId===>"+pingjiaId);
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		map = webClient.pingjia(pingjiaValue,pingjiaId);
		return map;
	}
	
	/**
	 * 点击保存按钮
	 * @return
	 */
	@RequestMapping(value = "baocun", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> baocun(HttpServletRequest request,HttpSession session) {
		log.info("===============点击保存按钮====================");
		Map<String, Object> map = new HashMap<String ,Object>();
		String pjxx = request.getParameter("pjxx").toString();
		byte[] base64decodedBytes1 = Base64.getDecoder().decode(pjxx);
		try {
			pjxx = new String(base64decodedBytes1, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		System.out.println("pjxx===>"+pjxx);
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		map = webClient.baocun(pjxx);
		return map;
	}
	
	/**
	 * 点击先评价，再提交按钮
	 * @return
	 */
	@RequestMapping(value = "tijiao", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> tijiao(HttpServletRequest request,HttpSession session) {
		log.info("===============点击先评价，再提交按钮====================");
		Map<String, Object> map = new HashMap<String ,Object>();
		String pjxx = request.getParameter("pjxx").toString();
		byte[] base64decodedBytes1 = Base64.getDecoder().decode(pjxx);
		try {
			pjxx = new String(base64decodedBytes1, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		System.out.println("pjxx===>"+pjxx);
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		map = webClient.tijiao(pjxx);
		return map;
	}
	
	
	
	/**
	 * 退出登录
	 * @param session
	 * @return
	 */
//	@RequestMapping(value = "quit", method = RequestMethod.GET)
//	public String quit(HttpSession session) {
//		Object obj = session.getAttribute(Csair.USERNAME);
//		if (obj != null) {
//			CsairImpl csair = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
//			csair.getWebClient().close();
//			log.info("重新打开首页");
//			session.setAttribute(Csair.WEBCLIENT, new CsairImpl());
//			session.removeAttribute(Csair.USERNAME);
//			session.removeAttribute(Csair.LOGINTYPE);
//		}
//		return "redirect:index";
//	}
}